package com.yaoweigroup.community.adapter;

import java.util.List;

import com.yaoweigroup.community.R;
import com.yaoweigroup.community.config.Constant;
import com.yaoweigroup.community.enity.TypeIcon;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class BMGridAdapter extends BaseAdapter {
	private LayoutInflater inflater;
	private List<TypeIcon> items;

	public BMGridAdapter(Context context, List<TypeIcon> items) {
		this.items = items;
		inflater = LayoutInflater.from(context);

	}

	@Override
	public int getCount() {
		return items.size();
	}

	@Override
	public Object getItem(int position) {
		return items.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = this.inflater.inflate(R.layout.grid_item_view, null);
			holder.iv = (ImageView) convertView.findViewById(R.id.item_iv);
			holder.tv = (TextView) convertView.findViewById(R.id.item_tv);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		TypeIcon type = items.get(position);
		if (type != null) {
			if (Constant.iconMap.get(type.getSort()) != null) {
				holder.iv
						.setImageResource(Constant.iconMap.get(type.getSort()));
			} else {
				holder.iv.setImageResource(R.drawable.bm_more);
			}
			holder.tv.setText(type.getTypename());
		}
		return convertView;
	}

	private class ViewHolder {
		ImageView iv;
		TextView tv;
	}

	public void setDataChange(List<TypeIcon> list) {
		this.items = list;
		notifyDataSetChanged();
	}

}
